package com.asset.salesforce.controller;

import java.util.List;
import java.util.Map;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.asset.salesforce.service.CalendarService;
import com.asset.salesforce.service.JoinRequestService;
import com.asset.salesforce.service.RoleShiroService;
import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.JoinRequest;
import com.asset.salesforce.domain.Role;
import com.asset.salesforce.domain.User;
import com.asset.salesforce.service.UserShiroService;

@Controller
public class ManageUsersController {

	@Autowired
	UserShiroService userShiroService;

	@Autowired
	RoleShiroService roleShiroService;

	@Autowired
	private JoinRequestService joinRequestService;
	
	@Autowired
	private CalendarService calendarService;
	
	@RequestMapping(value = "/manageUsers", method = RequestMethod.GET)
	public String header(Model model, HttpSession session) {
		Employee employee = (Employee)session.getAttribute("userDetails");

		List<User> users = userShiroService.getAllUsers(employee);
		List<Role> roles = roleShiroService.getAllRoles();
		User user = users.get(0);
		user.getRoles().contains(roles.get(0));
		model.addAttribute("users", users);
		model.addAttribute("roles", roles);
		joinRequestService.getJoinRquestNo(session, model);
		calendarService.getAppointmentEventsNo(session, model);
		calendarService.getCallEventssNo(session, model);
		return "manageUsers";
	}

	@RequestMapping(value = "/manageUsers", method = RequestMethod.POST)
	public String update(Model model, HttpSession session,
			HttpServletRequest request) {
		Map<String, String[]> params = request.getParameterMap();
		Employee employee = (Employee)session.getAttribute("userDetails");
		List<User> users = userShiroService.getAllUsers(employee);
		for (User user : users) {

			int id = user.getId();
			String owner = new String(id + "owner");
			String leadgenerator = new String(id + "leadgenerator");
			String salesmanager = new String(id + "salesmanager");
			String salesman = new String(id + "salesman");
			String bussinessdeveloper = new String(id + "bussinessdeveloper");
			//lead
			String leadAdd = new String(id + "leadAdd");
			String leadEdit = new String(id + "leadEdit");
			String leadDelete = new String(id + "leadDelete");
			String leadView = new String(id + "leadView");
			String leadConvert = new String(id + "leadConvert");
			String leadImport = new String(id + "leadImport");
			String leadFellow = new String(id + "leadFellow");
			//call and appointment
			String callAdd = new String(id + "callAdd");
			String callEdit = new String(id + "callEdit");
			String callView = new String(id + "callView");
			String appointmentAdd = new String(id + "appointmentAdd");
			String appointmentEdit = new String(id + "appointmentEdit");
			String appointmentView = new String(id + "appointmentView");
			//account
			String accountAdd = new String(id + "accountAdd");
			String accountEdit = new String(id + "accountEdit");
			String accountDelete = new String(id + "accountDelete");
			String accountView = new String(id + "accountView");
			String accountConvert = new String(id + "accountConvert");
			String accountFellow = new String(id + "accountFellow");
			//contact
			String contactAdd = new String(id + "contactAdd");
			String contactEdit = new String(id + "contactEdit");
			String contactDelete = new String(id + "contactDelete");
			String contactView = new String(id + "contactView");
			String contactFellow = new String(id + "contactFellow");
			//Stock Category
			String stockCategoryAdd = new String(id+"stockCategoryAdd");
			String stockCategoryView = new String(id+"stockCategoryView");
			String stockCategoryEdit = new String(id+"stockCategoryEdit");
			String stockCategoryDelete = new String(id+"stockCategoryDelete");
			//Stock Sub Category
			String subCategoryAdd = new String(id+"subCategoryAdd");
			String subCategoryView = new String(id+"subCategoryView");
			String subCategoryEdit = new String(id+"subCategoryEdit");
			String subCategoryDelete = new String(id+"subCategoryDelete");
			//Stock Product
			String stockProductAdd = new String(id+"stockProductAdd");
			String stockProductView = new String(id+"stockProductView");
			String stockProductEdit = new String(id+"stockProductEdit");
			String stockProductDelete = new String(id+"stockProductDelete");
			//Stock Sku
			String stockSKUAdd = new String(id+"stockSKUAdd");
			String stockSKUView = new String(id+"stockSKUView");
			String stockSKUEdit = new String(id+"stockSKUEdit");
			String stockSKUDelete = new String(id+"stockSKUDelete");
			//Invoice
			String invoiceAdd = new String(id+"invoiceAdd");
			String invoiceView = new String(id+"invoiceView");
			String invoiceTaxAdd = new String(id+"invoiceTaxAdd");
			String invoiceTaxView = new String(id+"invoiceTaxView");
			String invoiceTaxEdit = new String(id+"invoiceTaxEdit");
			String invoiceTaxDelete = new String(id+"invoiceTaxDelete");
			
			String viewAllTaxes = new String(id+"viewAllTaxes");
			String invoiceExport = new String(id+"invoiceExport");
			
			if (params.containsKey(leadgenerator)) {

				if ((params.get(leadgenerator)[0]).equals("on")) {

					if (!user.getRoles().contains(
							roleShiroService.findRole("leadgenerator"))) {
						List<Role> roles = user.getRoles();
						roles.add(roleShiroService.findRole("leadgenerator"));
						user.setRoles(roles);
						userShiroService.editUser(user);

					}
				}

			}else if(user.getRoles().contains(roleShiroService.findRole("leadgenerator"))){
				List<Role> roles = user.getRoles();
				roles.remove(roleShiroService.findRole("leadgenerator"));
				user.setRoles(roles);
				userShiroService.editUser(user);
			}

			if (params.containsKey(owner)) {

				if ((params.get(owner)[0]).equals("on")) {

					if (!user.getRoles().contains(
							roleShiroService.findRole("owner"))) {
						List<Role> roles = user.getRoles();
						roles.add(roleShiroService.findRole("owner"));
						user.setRoles(roles);
						userShiroService.editUser(user);

					}
				}
				 

			}else if(user.getRoles().contains(roleShiroService.findRole("owner"))){
				List<Role> roles = user.getRoles();
				roles.remove(roleShiroService.findRole("owner"));
				user.setRoles(roles);
				userShiroService.editUser(user);
			}

			if (params.containsKey(salesmanager)) {

				if ((params.get(salesmanager)[0]).equals("on")) {

					if (!user.getRoles().contains(
							roleShiroService.findRole("salesmanager"))) {
						List<Role> roles = user.getRoles();
						roles.add(roleShiroService.findRole("salesmanager"));
						user.setRoles(roles);
						userShiroService.editUser(user);

					}
				}

			}else if(user.getRoles().contains(roleShiroService.findRole("salesmanager"))){
				List<Role> roles = user.getRoles();
				roles.remove(roleShiroService.findRole("salesmanager"));
				user.setRoles(roles);
				userShiroService.editUser(user);
			}

			if (params.containsKey(salesman)) {

				if ((params.get(salesman)[0]).equals("on")) {

					if (!user.getRoles().contains(
							roleShiroService.findRole("salesman"))) {
						List<Role> roles = user.getRoles();
						roles.add(roleShiroService.findRole("salesman"));
						user.setRoles(roles);
						userShiroService.editUser(user);

					}
				}

			}else if(user.getRoles().contains(roleShiroService.findRole("salesman"))){
				List<Role> roles = user.getRoles();
				roles.remove(roleShiroService.findRole("salesman"));
				user.setRoles(roles);
				userShiroService.editUser(user);
			}

		
		
		if (params.containsKey(bussinessdeveloper)) {

			if ((params.get(bussinessdeveloper)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("bussinessdeveloper"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("bussinessdeveloper"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("bussinessdeveloper"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("bussinessdeveloper"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		
		if (params.containsKey(leadAdd)) {

			if ((params.get(leadAdd)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("leadAdd"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("leadAdd"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("leadAdd"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("leadAdd"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(leadEdit)) {

			if ((params.get(leadEdit)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("leadEdit"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("leadEdit"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("leadEdit"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("leadEdit"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(leadDelete)) {

			if ((params.get(leadDelete)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("leadDelete"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("leadDelete"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("leadDelete"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("leadDelete"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(leadConvert)) {

			if ((params.get(leadConvert)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("leadConvert"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("leadConvert"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("leadConvert"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("leadConvert"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(leadImport)) {

			if ((params.get(leadImport)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("leadImport"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("leadImport"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("leadImport"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("leadImport"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(leadFellow)) {

			if ((params.get(leadFellow)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("leadFellow"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("leadFellow"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("leadFellow"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("leadFellow"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(leadView)) {

			if ((params.get(leadView)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("leadView"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("leadView"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("leadView"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("leadView"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(callAdd)) {

			if ((params.get(callAdd)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("callAdd"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("callAdd"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("callAdd"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("callAdd"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(callEdit)) {

			if ((params.get(callAdd)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("callEdit"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("callEdit"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("callEdit"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("callEdit"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(callView)) {

			if ((params.get(callView)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("callView"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("callView"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("callView"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("callView"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(appointmentAdd)) {

			if ((params.get(appointmentAdd)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("appointmentAdd"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("appointmentAdd"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("appointmentAdd"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("appointmentAdd"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(appointmentEdit)) {

			if ((params.get(appointmentAdd)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("appointmentEdit"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("appointmentEdit"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("appointmentEdit"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("appointmentEdit"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(appointmentView)) {

			if ((params.get(appointmentView)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("appointmentView"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("appointmentView"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("appointmentView"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("appointmentView"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(accountAdd)) {

			if ((params.get(accountAdd)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("accountAdd"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("accountAdd"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("accountAdd"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("accountAdd"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(accountEdit)) {

			if ((params.get(accountEdit)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("accountEdit"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("accountEdit"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("accountEdit"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("accountEdit"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(accountDelete)) {

			if ((params.get(accountDelete)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("accountDelete"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("accountDelete"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("leadDelete"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("accountDelete"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(accountConvert)) {

			if ((params.get(accountConvert)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("accountConvert"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("accountConvert"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("accountConvert"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("accountConvert"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(accountFellow)) {

			if ((params.get(accountFellow)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("accountFellow"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("accountFellow"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("accountFellow"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("accountFellow"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(accountView)) {

			if ((params.get(accountView)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("accountView"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("accountView"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("accountView"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("accountView"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(contactAdd)) {

			if ((params.get(contactAdd)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("contactAdd"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("contactAdd"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("contactAdd"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("contactAdd"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(contactEdit)) {

			if ((params.get(contactEdit)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("contactEdit"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("contactEdit"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("contactEdit"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("contactEdit"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(contactDelete)) {

			if ((params.get(contactDelete)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("contactDelete"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("contactDelete"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("leadDelete"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("contactDelete"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(contactFellow)) {

			if ((params.get(contactFellow)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("contactFellow"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("contactFellow"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("contactFellow"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("contactFellow"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(contactView)) {

			if ((params.get(contactView)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("contactView"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("contactView"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("contactView"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("contactView"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		
		if (params.containsKey(stockCategoryAdd)) {

			if ((params.get(stockCategoryAdd)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("stockCategoryAdd"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("stockCategoryAdd"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("stockCategoryAdd"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("stockCategoryAdd"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		
		if (params.containsKey(stockCategoryView)) {

			if ((params.get(stockCategoryView)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("stockCategoryView"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("stockCategoryView"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("stockCategoryView"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("stockCategoryView"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		
		if (params.containsKey(stockCategoryEdit)) {

			if ((params.get(stockCategoryEdit)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("stockCategoryEdit"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("stockCategoryEdit"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("stockCategoryEdit"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("stockCategoryEdit"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		
		if (params.containsKey(stockCategoryDelete)) {

			if ((params.get(stockCategoryDelete)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("stockCategoryDelete"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("stockCategoryDelete"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("stockCategoryDelete"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("stockCategoryDelete"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		
		if (params.containsKey(subCategoryAdd)) {

			if ((params.get(subCategoryAdd)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("subCategoryAdd"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("subCategoryAdd"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("subCategoryAdd"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("subCategoryAdd"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		
		if (params.containsKey(subCategoryView)) {

			if ((params.get(subCategoryView)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("subCategoryView"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("subCategoryView"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("subCategoryView"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("subCategoryView"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		
		if (params.containsKey(subCategoryEdit)) {

			if ((params.get(subCategoryEdit)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("subCategoryEdit"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("subCategoryEdit"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("subCategoryEdit"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("subCategoryEdit"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		
		if (params.containsKey(subCategoryDelete)) {

			if ((params.get(subCategoryDelete)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("subCategoryDelete"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("subCategoryDelete"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("subCategoryDelete"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("subCategoryDelete"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		
		if (params.containsKey(stockProductAdd)) {

			if ((params.get(stockProductAdd)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("stockProductAdd"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("stockProductAdd"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("stockProductAdd"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("stockProductAdd"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		
		if (params.containsKey(stockProductView)) {

			if ((params.get(stockProductView)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("stockProductView"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("stockProductView"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("stockProductView"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("stockProductView"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		
		if (params.containsKey(stockProductEdit)) {

			if ((params.get(stockProductEdit)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("stockProductEdit"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("stockProductEdit"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("stockProductEdit"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("stockProductEdit"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		
		if (params.containsKey(stockProductDelete)) {

			if ((params.get(stockProductDelete)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("stockProductDelete"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("stockProductDelete"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("stockProductDelete"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("stockProductDelete"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		
		if (params.containsKey(stockSKUAdd)) {

			if ((params.get(stockSKUAdd)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("stockSKUAdd"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("stockSKUAdd"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("stockSKUAdd"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("stockSKUAdd"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		
		if (params.containsKey(stockSKUView)) {

			if ((params.get(stockSKUView)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("stockSKUView"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("stockSKUView"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("stockSKUView"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("stockSKUView"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		
		if (params.containsKey(stockSKUEdit)) {

			if ((params.get(stockSKUEdit)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("stockSKUEdit"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("stockSKUEdit"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("stockSKUEdit"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("stockSKUEdit"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		
		if (params.containsKey(stockSKUDelete)) {

			if ((params.get(stockSKUDelete)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("stockSKUDelete"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("stockSKUDelete"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("stockSKUDelete"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("stockSKUDelete"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(invoiceView)) {

			if ((params.get(invoiceView)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("invoiceView"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("invoiceView"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("invoiceView"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("invoiceView"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(invoiceTaxAdd)) {

			if ((params.get(invoiceTaxAdd)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("invoiceTaxAdd"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("invoiceTaxAdd"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("invoiceTaxAdd"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("invoiceTaxAdd"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(invoiceTaxView)) {

			if ((params.get(invoiceTaxView)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("invoiceTaxView"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("invoiceTaxView"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("invoiceTaxView"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("invoiceTaxView"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(invoiceTaxEdit)) {

			if ((params.get(invoiceTaxEdit)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("invoiceTaxEdit"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("invoiceTaxEdit"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("invoiceTaxEdit"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("invoiceTaxEdit"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(invoiceTaxDelete)) {

			if ((params.get(invoiceTaxDelete)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("invoiceTaxDelete"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("invoiceTaxDelete"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("invoiceTaxDelete"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("invoiceTaxDelete"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(viewAllTaxes)) {

			if ((params.get(viewAllTaxes)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("viewAllTaxes"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("viewAllTaxes"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("viewAllTaxes"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("viewAllTaxes"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(invoiceExport)) {

			if ((params.get(invoiceExport)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("invoiceExport"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("invoiceExport"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("invoiceExport"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("invoiceExport"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
		if (params.containsKey(invoiceAdd)) {

			if ((params.get(invoiceAdd)[0]).equals("on")) {

				if (!user.getRoles().contains(
						roleShiroService.findRole("invoiceAdd"))) {
					List<Role> roles = user.getRoles();
					roles.add(roleShiroService.findRole("invoiceAdd"));
					user.setRoles(roles);
					userShiroService.editUser(user);

				}
			}

		}else if(user.getRoles().contains(roleShiroService.findRole("invoiceAdd"))){
			List<Role> roles = user.getRoles();
			roles.remove(roleShiroService.findRole("invoiceAdd"));
			user.setRoles(roles);
			userShiroService.editUser(user);
		}
	}
		List<User> users2 = userShiroService.getAllUsers(employee);
		List<Role> roles = roleShiroService.getAllRoles();
		model.addAttribute("users", users2);
		model.addAttribute("roles", roles);
		joinRequestService.getJoinRquestNo(session, model);
		calendarService.getAppointmentEventsNo(session, model);
		calendarService.getCallEventssNo(session, model);
		return "manageUsers";
	}
}
